/**
 * @Author yinyunqi
 * @date 2018年1月11日
 * @Content 
 */
package com.yongqi.service.console;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.Magic;
import com.github.abel533.echarts.code.Orient;
import com.github.abel533.echarts.code.PointerType;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.code.X;
import com.github.abel533.echarts.code.Y;
import com.github.abel533.echarts.feature.MagicType;
import com.github.abel533.echarts.series.Line;
import com.yongqi.mapper.console.BillMapper;
import com.yongqi.model.Family;
import com.yongqi.model.console.Bill;
import com.yongqi.pub.DateFormat;
import com.yongqi.pub.DoubleFormat;

@Service
public class MainService {
    @Autowired
    BillMapper billMapper;
    /**
     * 构建主页面折线图
     * @return
     */
    public Option lineChart() {
	//一年间月份
	String thisYearMonth = DateFormat.dateFormate4();//上个月
	String lastYearMonth = DateFormat.dateFormate3();//上年这个月
	Map<String, String> map = new HashMap<String, String>();
	map.put("lastYearMonth", lastYearMonth);
	map.put("thisYearMonth", thisYearMonth);
	List<String> monthList = billMapper.getYearMonth(map);//上年这个月到上个月
	StringBuffer monthResult = new StringBuffer();//存放拼接后的日期
	for (String month : monthList) {
	    //拼接年月数据
	    monthResult.append(month).append("@");//@JSON转化时替换为","
	}
	monthResult.delete(monthResult.length()-1, monthResult.length());//删除拼接后年月尾部的@
	
	String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
	List<Double> incomeList = new ArrayList<Double>(); 
	List<Double> payList = new ArrayList<Double>(); 
	for (String month : monthList) {
//	    Map<String, String> conMap = new HashMap<String, String>();//存放年和家庭账号条件
	    map.put("familyAccount", familyAccount);
	    map.put("time", month);
	    map.put("billType", "income");
	    List<Bill> incomeBill = billMapper.billBarSelect(map);
	    Double tempIncome= 0.00;
	    for (Bill income : incomeBill) {
		tempIncome += Double.valueOf(income.getMoney());
	    }
	    incomeList.add(tempIncome);
	    map.put("billType", "pay");
	    List<Bill> payBill = billMapper.billBarSelect(map);
	    Double tempPay= 0.00;
	    for (Bill pay : payBill) {
		tempPay += Double.valueOf(pay.getMoney());
	    }
	    payList.add(tempPay);
	}
	//拼接收入数据串
	StringBuffer incomeResult = new StringBuffer();
	for(int i=0;i<incomeList.size();i++) {
	    incomeResult.append(incomeList.get(i)).append("@");
	}
	incomeResult.delete(incomeResult.length()-1, incomeResult.length());
	//拼接支出数据串
	StringBuffer payResult = new StringBuffer();
	for(int i=0;i<payList.size();i++) {
	    payResult.append("-").append(payList.get(i)).append("@");
	}
	//拼接净收入数据串
	StringBuffer pureIncomeResult = new StringBuffer();
	for(int i=0;i<payList.size();i++) {
	    Double pureIncome = incomeList.get(i)-payList.get(i);
	    pureIncomeResult.append(pureIncome).append("@");
	}
	payResult.delete(payResult.length()-1, payResult.length());
	pureIncomeResult.delete(pureIncomeResult.length()-1, pureIncomeResult.length());
	Option option = new Option();
	option.title(lastYearMonth+"到"+thisYearMonth+"财政变化");
	//提示框组件。
	option.tooltip(Trigger.axis);
	option.tooltip().axisPointer().type(PointerType.cross);
	//图例组件展现了不同系列的标记(symbol)，颜色和名字。可以通过点击图例控制哪些系列不显示。
	option.legend().orient(Orient.horizontal).x(X.center).y(Y.top).data("收入","支出","净收入");
	//工具栏。内置有导出图片，数据视图，动态类型切换，数据区域缩放，重置五个工具。
	option.toolbox().feature(Tool.saveAsImage,
		Tool.mark,		//辅助线开关
		Tool.dataView,		//数据视图工具，可以展现当前图表所用数据，编辑后可以动态更新
		new MagicType(Magic.line,Magic.bar),	//动态类型切换
		Tool.restore
		);
	//直角坐标系内绘图网格，单个 grid 内最多可以放置上下两个 X 轴，左右两个 Y 轴。可以在网格上绘制折线图，柱状图，散点图（气泡图）。
	option.grid().left("3%").right("4%").bottom("3%").containLabel(true);
	//直角坐标系 grid 中的 x 轴，一般情况下单个 grid 组件最多只能放上下两个 x 轴，多于两个 x 轴需要通过配置 offset 属性防止同个位置多个 x 轴的重叠。
	//类目轴，适用于离散的类目数据，为该类型时必须通过 data 设置类目数据。
	CategoryAxis categoryAxis = new CategoryAxis();
	categoryAxis.type(new CategoryAxis().type().category).boundaryGap(false);
	categoryAxis.data(monthResult);
	option.xAxis(categoryAxis);
	//直角坐标系 grid 中的 y 轴，一般情况下单个 grid 组件最多只能放左右两个 y 轴，多于两个 y 轴需要通过配置 offset 属性防止同个位置多个 Y 轴的重叠。
	ValueAxis valueAxis = new ValueAxis();
	valueAxis.type(new ValueAxis().type().value);
	option.yAxis(valueAxis);
	//先构建单个折线图然后组合
	Line incomeLine = new Line();
	incomeLine.name("收入");
//	incomeLine.stack("总量");
//	incomeLine.itemStyle().normal().areaStyle().type("default");
	incomeLine.data(incomeResult);
	Line payLine = new Line();
	payLine.name("支出");
//	payLine.stack("总量");
//	payLine.itemStyle().normal().areaStyle().type("default");
	payLine.data(payResult);
	Line pureIncomeLine = new Line();
	pureIncomeLine.name("净收入");
//	pureIncomeLine.stack("总量");
	pureIncomeLine.itemStyle().normal().areaStyle().type("default");
	pureIncomeLine.data(pureIncomeResult);
	option.series(incomeLine,payLine,pureIncomeLine);
	return option;
    }
    /**
     * 收入/支出
     * @return
     */
    public String currentBill(String billType){
	String month = DateFormat.dateFormate2();
	String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
	Map<String, String> map = new HashMap<String, String>();
	map.put("time",month);
	map.put("familyAccount", familyAccount);
	map.put("billType", billType);
	List<Bill> billList = billMapper.billBarSelect(map);
	Double billTotal = 0.00;
	for (Bill bill : billList) {
	    billTotal += Double.valueOf(bill.getMoney());
	}
	String result = DoubleFormat.format1(billTotal);
	return result;
    }
    /**
     * 计算近12月支出均值
     * @return
     */
    public String avgBudget() {
    	//一年间月份
    	String thisYearMonth = DateFormat.dateFormate4();//上个月
    	String lastYearMonth = DateFormat.dateFormate3();//上年这个月
    	String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
    	Map<String, String> map = new HashMap<String, String>();
    	map.put("lastYearMonth", lastYearMonth);
    	map.put("thisYearMonth", thisYearMonth);
    	map.put("familyAccount", familyAccount);
    	map.put("billType","pay");
    	Double paySum = billMapper.billAvg(map);
    	String result = DoubleFormat.format1(paySum);
    	return result;
    }
}

